package com.yunhuakeji.wddx.utils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;

public class Datehelper {
	public static DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
	public static DateFormat NYR= new SimpleDateFormat("yyyyMMdd");
	public static SimpleDateFormat sdf =   new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	public static boolean sub(Date time1,Date time2,long error)
	{
		long a=Math.abs((time1.getTime() - time2.getTime()))/1000;
	
		return a/10000<error;
	}
	
	public static long sjc(String start,String end)
	{

		try
		{
		Date d1 = df.parse(start);
		Date d2 = df.parse(end);
		return  d2.getTime() - d1.getTime();
		}
		catch (Exception e)
		{
		}
		return 0;
	}
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static HashMap djzandxqj(Date start,Date now)
	{
		HashMap m= new HashMap();
		Calendar nowcal = Calendar.getInstance();
		nowcal.setTime(now);
		m.put("xqj", nowcal.get(Calendar.DAY_OF_WEEK)-1);
		
		Calendar startcal= Calendar.getInstance();
		startcal.setTime(start);
		int xq=0;
		int days=0;
		//days=nowcal.get(Calendar.DAY_OF_YEAR)-startcal.get(Calendar.DAY_OF_YEAR)-(7-startcal.get(Calendar.DAY_OF_WEEK)-1);
	    while(startcal.before(nowcal)){
	            days++;
	            startcal.add(Calendar.DAY_OF_YEAR, 1);
	   }

		if(days>=0)
		{
			xq=days/7+1;
		}
		else xq=0;
		m.put("djz", xq);
		return m;

	}
	
	// 锟斤拷玫锟斤拷锟�锟斤拷时锟斤拷
		public static Date getTimesmorning() {
			Calendar cal = Calendar.getInstance();
			cal.set(Calendar.HOUR_OF_DAY, 0);
			cal.set(Calendar.SECOND, 0);
			cal.set(Calendar.MINUTE, 0);
			cal.set(Calendar.MILLISECOND, 0);
			return cal.getTime();
		}

		// 锟斤拷玫锟斤拷锟�4锟斤拷时锟斤拷
		public static Date getTimesnight() {
			Calendar cal = Calendar.getInstance();
			cal.set(Calendar.HOUR_OF_DAY, 24);
			cal.set(Calendar.SECOND, 0);
			cal.set(Calendar.MINUTE, 0);
			cal.set(Calendar.MILLISECOND, 0);
			return  cal.getTime();
		}

		// 锟斤拷帽锟斤拷锟揭�锟斤拷时锟斤拷
		public static Date getTimesWeekmorning() {
			Calendar cal = Calendar.getInstance();
			cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
			cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
			return  cal.getTime();
		}

		// 锟斤拷帽锟斤拷锟斤拷锟�4锟斤拷时锟斤拷
		public  static Date getTimesWeeknight() {
			Calendar cal = Calendar.getInstance();
			cal.setTime(getTimesWeekmorning());
			cal.add(Calendar.DAY_OF_WEEK, 7);
			return cal.getTime();
		}

		// 锟斤拷帽锟斤拷碌锟揭伙拷锟�锟斤拷时锟斤拷
		public static Date getTimesMonthmorning() {
			Calendar cal = Calendar.getInstance();
			cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
			cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
			return  cal.getTime();
		}

		// 锟斤拷帽锟斤拷锟斤拷锟斤拷一锟斤拷24锟斤拷时锟斤拷
		public static Date getTimesMonthnight() {
			Calendar cal = Calendar.getInstance();
			cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
			cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
			cal.set(Calendar.HOUR_OF_DAY, 24);
			return cal.getTime();
		}
		public static String lastWeek(){
			   Date date = new Date();
			   int year=Integer.parseInt(new SimpleDateFormat("yyyy").format(date));
			   int month=Integer.parseInt(new SimpleDateFormat("MM").format(date));
			   int day=Integer.parseInt(new SimpleDateFormat("dd").format(date))-6;
			  
			   if(day<1){
			    month-=1;
			    if(month==0){
			     year-=1;month=12;
			    }
			    if(month==4||month==6||month==9||month==11){
			     day=30+day;
			    }else if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
			    {
			     day=31+day;
			    }else if(month==2){
			     if(year%400==0||(year %4==0&&year%100!=0))day=29+day;
			     else day=28+day;
			    }     
			   }
			   String y = year+"";String m ="";String d ="";
			   if(month<10) m = "0"+month;
			   else m=month+"";
			   if(day<10) d = "0"+day;
			   else d = day+"";
			  
			   return y+m+d;
			}

}
